package com.china08.yxyapi.api.manage.sch.platform.weikecheng;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.es.ElasticYxyResResource;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.security.profile.YxyProfile;
import com.china08.yxyapi.service.yxweike.YxyWeikeService;
import com.china08.yxyapi.util.EsResourcePvUpdateUtil;
import com.china08.yxyapi.util.YxyPlayUrlForWeiKechengBuilder;

/**
 * 微课详情页
 * 
 */
@CrossOrigin
@RestController("SchWeikechengDetailController")
public class WeikechengDetailController extends YxyApiBaseController {

    @Value("${yxy.res.url}")
    private String yxy_res_url;

    @Autowired
    private YxyWeikeService weikeService;

    @Autowired
    private YxyPlayUrlForWeiKechengBuilder builder;

    @ResponseBody
    @RequestMapping(value = "/api_yw/manage/sch/platform/weikecheng/detail", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(HttpServletRequest request, HttpServletResponse response,
            @RequestParam(value = "weikeId", required = true) String weikeId) {
        // RespModel model = new RespModel();

        YxyProfile profile = getProfile(request, response);
        ElasticYxyResResource weike = weikeService.findOneResource(weikeId);
        RespModel resp = new RespModel();
        resp.title = weike.getTitle();
        resp.intro = weike.getIntro();
        resp.weikeId = weike.getId();
        // 分类
        List<String> categoryList = new ArrayList<String>();
        String category = "";
        if (StringUtils.isNotBlank(weike.getStageName())) {
            categoryList.add(weike.getStageName());
        }

        if (StringUtils.isNotBlank(weike.getWeikeSubjectName())) {
            categoryList.add(weike.getWeikeSubjectName());
        }

        if (CollectionUtils.isNotEmpty(weike.getKnowNames())) {
            categoryList.addAll(weike.getKnowNames());
        }
        category = StringUtils.join(categoryList, ",");
        resp.category = category;
        String viewScope = "";
        if (weike.getViewScope() == 2) {
            viewScope = "共享到教育云";
        }
        if (weike.getViewScope() == 1) {
            viewScope = "共享到学校";
        }
        if (weike.getViewScope() == 0) {
            viewScope = "私有";
        }
        resp.viewScope = viewScope;
        resp.cover = weike.getCover();
        String url = builder.setResourceId(weikeId).setUserId(profile.getId())
                .setUserVip(Boolean.valueOf(profile.getUserVip())).setResVip(Boolean.valueOf(profile.getResVip()))
                .setViewFrom("weikecheng").build();
        resp.location = url;
        // resp.location = yxy_res_url + "/res/browse/" + weikeId;
        resp.convertStatus = weike.getConvertStatus();
        resp.setMessage("成功");
        EsResourcePvUpdateUtil.updateEsResoure(weike.getId() + "_1");
        return resp;
    }

    protected static class RespModel extends YxyApiRespModel {

        public String weikeId;// 微课id

        public String title;// 微课名称

        public String intro;// 微课简介

        public String category;// 知识点,知识点...

        public String viewScope;// 上传位置

        public String cover;// 微课封面

        public String location;// 微课路径

        public Integer convertStatus;// 文件的状态信息 0初始上传， 1 转码中 ，2 转码成功 ，3 转码失败

    }

}
